# -*- coding:utf-8 -*-
"""
该脚本为挂载222数据库服务器脚本，用来接受从cgtw传来的镜头同步信息
调用TD库来实现任务更新

"""
import socket

class UpdateServer(object):
    """
    服务：接受从cgtw传来的信息，即使更新本地信息
    """
    def __init__(self,HOST,PORT,BUFFER):
        super(UpdateServer,self).__init__()

        self.HOST = HOST
        self.PORT = PORT
        self.BUFFER = BUFFER
        # self.cgtw_connect = psycopg2.connect(database=origin_config['database'], user=origin_config['user'], password=origin_config['password'],
        #                                 host=origin_config['host'],port=origin_config['port'])
        #
        # self.cgtw_cur =self.cgtw_connect.cursor()
        # self.time_connect= psycopg2.connect(database=target_config['database'], user=target_config['user'], password=target_config['password'],
        #                                 host=target_config['host'],port=target_config['port'])
        # self.time_cur=self.time_connect.cursor()

    def init_connect(self):
        """
        创建数据库连接
        :return:
        """
        # self.cgtw_connect = psycopg2.connect(database=origin_config['database'], user=origin_config['user'], password=origin_config['password'],
        #                                 host=origin_config['host'],port=origin_config['port'])
        #
        # self.cgtw_cur =self.cgtw_connect.cursor()
        # self.time_connect= psycopg2.connect(database=target_config['database'], user=target_config['user'], password=target_config['password'],
        #                                 host=target_config['host'],port=target_config['port'])
        # self.time_cur=self.time_connect.cursor()

    def runServer(self):
        """
        开启服务
        :param host:
        :param port:
        :param buffer:
        :return:
        """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((self.HOST, self.PORT))
        sock.listen(100)
        # self.load_debug('启动服务')
        print('servering listening')

        while True:
            try:
                client_sock, client_addr = sock.accept()
                recv = client_sock.recv(self.BUFFER)
                # print(chardet.detect(recv))
                # print(recv)
                if recv:
                    data = eval(recv)
                    print(data)
                    database = data['database']
                    module = data['module']
                    task_id_list = data['task_id_list']
                    link_id_list = data['link_id_list']
                    connect_link_id = '|'.join(link_id_list)
                    # # self.init_connect()  # 每次生成一个handler
                    try:
                    #     if module == 'asset':
                    #         self.synchronized_R_asset(database, connect_link_id)
                    #         print('同步r_asset成功')
                    #     elif module == 'shot':
                    #         self.synchronized_R_shot(database, connect_link_id)
                    #         print('同步r_shot成功')
                    #     self.synchronized_R_task(database, task_id_list)  # 同步本地r_task
                    #     print('同步完成了')
                    #     client_sock.send('success'.encode('utf-8'))
                        client_sock.send('failed'.encode('utf-8'))
                        print('返回成功信息')
                    except Exception as synchronized_ERR:
                        print('同步失败')
                        print(str(synchronized_ERR))
                        client_sock.send('failed')
            except Exception as accept_ERR:
                print('接收失败失败')
                print(str(accept_ERR))


if __name__ == '__main__':
    HOST = '192.168.1.85'
    PORT = 24601
    BUFFER = 1024
    us = UpdateServer(HOST,PORT,BUFFER)
    us.runServer()